home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 2: CDPD 1
/
Almathera Ten on Ten - Disc 2: CDPD 1.iso
/
pd
/
101-125
/
105
/
bison
/
calc.y
< prev
next >
Wrap
Text File
|
1995-03-13
|
2KB
|
117 lines
%{
#include <ctype.h>
int regs[26];
int base;
int val;
int printflag;
%}
%start stmt
%token DIGIT LETTER EXIT
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS
%%
stmt : expr
{ val = $1; printflag = 1; }
| LETTER '=' expr
{ regs[$1] = $3; }
| EXIT
{ exit(0); }
;
expr : '(' expr ')'
{ $$ = $2; }
| expr '+' expr
{ if ($1 == 69)
{
$$ = 2;
printf ("$1 became %d\n", $1);
$1 = 69;
}
$$ = $1 + $3; }
| expr '-' expr
{ $$ = $1 - $3; }
| expr '*' expr
{ $$ = $1 * $3; }
| expr '/' expr
{ $$ = $1 / $3; }
| expr '%' expr
{ $$ = $1 % $3; }
| expr '|' expr
{ $$ = $1 | $3; }
| expr '&' expr
{ $$ = $1 & $3; }
| '-' expr %prec UMINUS
{ $$ = - $2; }
| LETTER
{ $$ = regs[$1]; }
| number
/* | '?'
{ yydebug = !yydebug; }
*/ ;
number : DIGIT
{ $$ = $1; base = ($1 == 0) ? 8 : 10; }
| number DIGIT
{ $$ = base * $1 + $2; }
;
%%
static int eol;
int
yylex()
{
int c;
while ( (c=getchar()) == ' ') {}
if (c == '\n')
{ eol = 1;
return 0; }
if (c == 'Q')
return(EXIT);
if (islower(c))
{
yylval = c - 'a';
return (LETTER);
}
if (isdigit(c))
{
yylval = c - '0';
return (DIGIT);
}
return (c);
}
yyerror(s)
char *s;
{
printf("%s\n", s);
}
main()
{
for (;;)
{
eol = 0;
printflag = 0;
if (yyparse()) printflag = 0;
if (printflag) printf("%d\n", val);
while (!eol) yylex();
}
}